﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Resources;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Resourctes
{
    /// <summary>
    /// 资源管理修改
    /// </summary>
    public class ResourceUpdateHandler : IRequestHandler<ResourceUpdateCommend, Result>
    {
        private readonly IBaseRepository<Resource> Resource;
        private readonly IMapper mapper;

        public ResourceUpdateHandler(IBaseRepository<Resource> Resource, IMapper mapper)
        {
            this.Resource = Resource;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(ResourceUpdateCommend request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Delete };

            var AddData = mapper.Map<Resource>(request);
            string sql = "UPDATE Resource SET ResourceNo=@ResourceNo ,ResourceName=@ResourceName ,ResourceTypeId=@ResourceTypeId ,ResourceDescription=@ResourceDescription " +
                         ",ResourceUnit=@ResourceUnit ,ResourceCost=@ResourceCost,ExpensesId=@ExpensesId," +
                         "ResourceCalculation =@ResourceCalculation ,BenchmarkId =@BenchmarkId,ResourcePrice =@ResourcePrice,ApprovalStatus =0 WHERE ResourceId =@ResourceId";
            var list=Resource.Update(sql, new
            {
                ResourceId = AddData.ResourceId,
                ResourceNo = AddData.ResourceNo,
                ResourceName = AddData.ResourceName,
                ResourceTypeId = AddData.ResourceTypeId,
                ResourceDescription = AddData.ResourceDescription,
                ResourceUnit = AddData.ResourceUnit,
                ResourceCost = AddData.ResourceCost,
                ExpensesId = request.ExpensesId,
                ResourceCalculation = request.ResourceCalculation,
                BenchmarkId = request.BenchmarkId,
                ResourcePrice = request.ResourcePrice,
                ApprovalStatus=request.ApprovalStatus,

            });
            if (list != null)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "修改成功";
            }

            return results;
        }
    }
}
